<?php
	class CatgoryControl extends CommonControl{

		function __auto() {
			$sql1="select value from ec_config where cid=4";
			$style=M()->query($sql1);
	        $template = __ROOT__ . '/template/' . $style[0]['value'];
	        $this->assign("template", $template);

    	}
    

		function index(){
			$db=M('category');
			$total =$db->count();
			// echo $total;die;
			$page = new page($total);
			$result = $db->findall($page->limit());
			// $data=Data::channel($result,"cid","pid",0,'',2,"--");
			$this->assign('data',$result);
			$this->display();
			echo $page->show();
		}

		function addshow(){
			$this->display();
		}

		function runadd(){
			$db=M('category');
			if ($db->add()) {
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			};
		}

		function addcshow(){
			$cid=$_GET['cid'];
			$db=M('category');
			$result=$db->where('cid="'.$cid.'"')->find();
			$this->assign('cid',$cid);
			$this->assign('result',$result);
			$this->display();
		}

		function runcadd(){
			$db=M('category');
			if ($db->add()) {
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			};
		}

		function editshow(){
			$cid=$_GET['cid'];
			$db=M('category');
			$result=$db->where('cid="'.$cid.'"')->find();
			$this->assign('cid',$cid);
			$this->assign('result',$result);
			$this->display();
		}

		function runedit(){
			$db=M('category');
			if ($db->save()) {
	            $this->success("编辑栏目成功", "index");
	        } else {
	            $this->error("栏目修改失败");
	        }
		}

		function catgorydel(){
			$cid=$_POST['cid'];
				$db=M('category');
				if($db->where("pid=" . $cid)->find()){
					// echo "	<script>
					// 			alert('请先删除子栏目');
					// 			window.location='http://localhost/cms0121/admin.php/catgory/index';
					// 		</script>";
					echo json_encode(array('status' => 0, 'msg' => '请先删除子栏目'));
				}else if($db->table('arc')->find("cid=" . $cid)){
					// echo "	<script>
					// 			alert('请先删除栏目文章');
					// 			window.location='http://localhost/cms0121/admin.php/catgory/index';
					// 		</script>";
					echo json_encode(array('status' => 0, 'msg' => '请先删除栏目里文章'));			
				}else {
					$db->del($cid);
					// echo "	<script>
					// 			alert('删除成功');
					// 			window.location='http://localhost/cms0121/admin.php/catgory/index';
					// 		</script>";
					echo json_encode(array('status' => 1, 'msg' => '删除成功'));			
				}
		}

		

	}
?>